package pkg.hicph.org;

import org.xmlpull.v1.XmlPullParser;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class Utils {
	
	// Preference file
    public static final String PREF_FILE = "pf_file";
    private static SharedPreferences preferences;

	// GAME STATES
    public final static int CREATED         = 0;
    public final static int NORMAL			= 1;
    public final static int NEW_GAME        = 2;
    public final static int CONTINUE_GAME   = 3;
    public final static int ENDED           = 4;
    public final static int RESULTS         = 5;
    public final static int END_OF_DAY		= 6;
    
    
    
    public final static int NEWLY_CREATED			= 10;
    
    public final static int START_HUNGRY_MODE		= 20;
    public final static int PLAY_HUNGRY_MODE		= 21;
    public final static int PAUSE_HUNGRY_MODE		= 22;
    public final static int END_HUNGRY_MODE			= 23;
    
    public final static int START_MONEY_PROBLEM		= 30;
    public final static int END_MONEY_PROBLEM		= 31;
    
    public final static int START_BEG_MODE			= 40;
    public final static int PLAY_BEG_MODE			= 41;
    public final static int END_BEG_MODE			= 42;
    
    public final static int START_HF_MODE			= 50;
    public final static int PLAY_HF_MODE			= 51;
    public final static int END_HF_MODE				= 52;
    
    public final static int START_RECYCLE_MODE		= 60;
    public final static int PLAY_RECYCLE_MODE		= 61;
    public final static int END_RECYCLE_MODE		= 62;
    
    public final static int START_SLEEP_MODE		= 70;
    public final static int PLAY_SLEEP_MODE			= 71;
    public final static int END_SLEEP_MODE			= 72;
    
    public final static int START_EAT_MODE			= 90;
    public final static int PLAY_EAT_MODE			= 91;
    public final static int END_EAT_MODE			= 92;
    public final static int PAUSE_EAT_MODE			= 93;
    
    public final static int END_GAME_SCREEN			= 80;
    
    
    public final static int MORNING		= 0;
    public final static int AFTERNOON	= 1;
    public final static int EVENING		= 2;
    public final static int NIGHT		= 3;
    
    // PLACES    
    public static Place[] eatingPlaces = new Place[10];
    public static int	  eatingPlacesCount = 0;
    
    public static Place[] beggingPlaces = new Place[10];
    public static int	  beggingPlacesCount = 0;
    
    public static Place[] husForbiPlaces = new Place[20];
    public static int	  husForbiPlacesCount = 0;
    
    public static Place[] trashCansPlaces = new Place[30];
    public static int	  trashCansPlacesCount = 0;
    
    public static Place[] sleepingPlaces = new Place[10];
    public static int	  sleepingPlacesCount = 0;
    
    public static Place activePlace = new Place();
    
    
    // GAME LOGIC
    public static int gamePhase = NEW_GAME;
    
    public static float foodDegradation;
    public static float sleepDegradation;
    public static float minPH;
    public static float minMH;
    public static float minMO;
    
    // DAY
    public static int currentTimeOfDay = MORNING;
    public static int currentDay = 1;
    
    // ACTIONS
    public static Action[] globalActions = new Action[300];
    public static int totalActions = 0;
    public static Action nextAction = new Action();
    
    // MEMORIES
    public static Memory[] globalMemories = new Memory[100];
    public static int totalMemories = 0;
    public static int achievedInSession = 0;
    
    // EVENTS
    public static Event[] globalEvents = new Event[50];
    public static int totalEvents = 0;
    public static Event activeEvent = new Event();
    
    //Trash bins visited
    public static String visitedBins = new String();
    
    // BEGGING
    public static int begChances = 0;
    public static int begCount = 0;
    public static Boolean isBegging = false;
    public static int oldMoney = 0;
    
    // SLEEP MODE
    public static int sleepSafety = 0;
    
    //Memories room
    public static Memory activeMemory = new Memory();
    
    
    // STORY CONNECTORS
    
    // ============================================== HAPPY ACTIONS
    public static String[] workHappyConnectors = {
		" Working cheered you up!", 
		" This helped you feel better.", 
		" You hoped you can find more work tomorrow.", 
		" It felt good to do something more productive.", 
		" Doing this made you feel a bit empowered.", 
		" You felt a bit tired, but happy.",
		"", 
		"", 
		"", 
		""};
    
    public static String[] funHappyConnectors = {
		" It was good to enjoy life a bit!", 
		" You really needed some joy.", 
		" To laugh helped you relax.", 
		" Some relaxing was more than welcome.", 
		" You could think on good things while having fun.", 
		" Relaxing can really help sometimes.",
		" Some good laughs and you felt a bit renewed.", 
		"", 
		"", 
		""};
    
    public static String[] eatHappyConnectors = {
		" Sometimes, some food really makes you happier.", 
		" You felt ready to take on the world!", 
		" Then, for a moment, your worries went away.", 
		" 'Hmm', you said, 'that was a good meal!'", 
		" A full belly, a better mood.", 
		" You felt ready to keep going!",
		" You felt lighter, despite the food!", 
		" Digesting cleared your mind for a while.", 
		" You smiled after the meal and thought about what to do next", 
		" Then, a sleepy and warm feeling overtook you."};
    
    public static String[] sleepHappyConnectors = {
		" It was good to have some rest after a full day.", 
		" You had some well-deserved rest.", 
		" Sleeping, you dreamed of better days to come.", 
		" You knew you would be fresh for tomorrow.", 
		" You really needed some quiet and cool time.", 
		" It was relaxing to get to sleep.",
		"", 
		"", 
		"", 
		""};
    
    //=================================================== NORMAL ACTIONS
    
    public static String[] workNormalConnectors = {
		" You felt some work helps, but not much.", 
		" Normal work, nothing special.", 
		" You thought that tomorrow will probably be like this.", 
		" Some really needed money, that was it.", 
		" 'Always these same things', you felt.", 
		"", 
		" Normal work, nothing special.", 
		" You thought that tomorrow will probably be like this.", 
		"", 
		" 'Always these same things', you felt."};
    
    public static String[] funNormalConnectors = {
		" 'Some relaxing, but life goes one', you thought.", 
		" You were still worried, but it was good to have some fun.", 
		" 'I wish I could do it more often', you thought.", 
		" You hoped you would be more relaxed in the future.", 
		" You felt a bit lighter, ready to keep going.", 
		"", 
		" You were still worried, but it was good to have some fun.", 
		" 'I wish I could do it more often', you thought.", 
		"", 
		" You felt a bit lighter, ready to keep going."};
    
    public static String[] eatNormalConnectors = {
		" It was good to have some food.", 
		" You thought that the next meal could be better.", 
		" You thought it would be good to not have to worry that much about food.", 
		" 'Hmm', you said, 'food.'", 
		" A full belly, digestion, and keep going.", 
		" It was good to have some food.", 
		"", 
		" You thought it would be good to not have to worry that much about food.", 
		" 'Hmm', you said, 'food.'", 
		""};
    
    public static String[] sleepNormalConnectors = {
		" You had a normal night of sleep.", 
		" You were a bit restless, but managed to sleep.", 
		" You had no dreams in that, that you remembered.", 
		" You thought 'I need to sleep to be good for tomorrow'.", 
		" Tired, you really needed some quiet time.", 
		"", 
		"", 
		" You had no dreams in that, that you remembered.", 
		"", 
		" Tired, you really needed some quiet time."};
    
//=================================================== SAD ACTIONS
    
    public static String[] workSadConnectors = {
		" You felt some work helps, but not much.", 
		" Normal work, nothing special.", 
		" You thought that tomorrow will probably be like this.", 
		" Some really needed money, that was it.", 
		" 'Always these same things', you felt.", 
		"", 
		" Normal work, nothing special.", 
		" You thought that tomorrow will probably be like this.", 
		"", 
		" 'Always these same things', you felt."};
    
    public static String[] funSadConnectors = {
		" 'Some relaxing, but life goes one', you thought.", 
		" You were still worried, but it was good to have some fun.", 
		" 'I wish I could do it more often', you thought.", 
		" You hoped you would be more relaxed in the future.", 
		" You felt a bit lighter, ready to keep going.", 
		"", 
		" You were still worried, but it was good to have some fun.", 
		" 'I wish I could do it more often', you thought.", 
		"", 
		" You felt a bit lighter, ready to keep going."};
    
    public static String[] eatSadConnectors = {
		" It was good to have some food.", 
		" You thought that the next meal could be better.", 
		" You thought it would be good to not have to worry that much about food.", 
		" 'Hmm', you said, 'food.'", 
		" A full belly, digestion, and keep going.", 
		" It was good to have some food.", 
		"", 
		" You thought it would be good to not have to worry that much about food.", 
		" 'Hmm', you said, 'food.'", 
		""};
    
    public static String[] sleepSadConnectors = {
		" You had a normal night of sleep.", 
		" You were a bit restless, but managed to sleep.", 
		" You had no dreams in that, that you remembered.", 
		" You thought 'I need to sleep to be good for tomorrow'.", 
		" Tired, you really needed some quiet time.", 
		"", 
		"", 
		" You had no dreams in that, that you remembered.", 
		"", 
		" Tired, you really needed some quiet time."};
    
    
    // PLAYER
    public static Player player = new Player();
	public static boolean isSelling = false;
	public static int sellChances = 0;
	protected static int sellCount = 0;
	public static boolean canShowStories = false;
	public static boolean isTimer;
    
 
	public static void showToastShort(Context base, String p) {
		Toast toast = Toast.makeText(base, p, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.BOTTOM, 0, 0);
		toast.show();
	}
	
	public static void showToastLong(Context base, String p) {
		Toast toast = Toast.makeText(base, p, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	
	public static void showTopToastShort(Context base, String p) {
		Toast toast = Toast.makeText(base, p, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP, 0, 20);
		toast.show();
	}
	
	public static Memory getMemoryByName(String param){
		
		for(int i = 0; i < Utils.totalMemories; i++){
			
			if(param.equalsIgnoreCase(Utils.globalMemories[i].name)){
				return Utils.globalMemories[i];
			}
	
		}
		
		return new Memory();
	}
	
	public static Event getEventByName(String param){
		
		for(int i = 0; i < Utils.totalEvents; i++){
			
			if(param.equalsIgnoreCase(Utils.globalEvents[i].name)){
				return Utils.globalEvents[i];
			}
	
		}
		
		return new Event();
	}
	
	public static void loadPlacesFromXML(Context pContext){
    	
		Place auxPlace = new Place();
	    XmlPullParser parser = pContext.getResources().getXml(R.xml.placesredesign);
	    int counter = 0;
	    
        try {
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
            	
                String name = parser.getName();
                int id = -1;
                String placeName = null;
                Double lat = -1.0;
                Double lon = -1.0;
                String action = "", desc = "";
                
                if((name != null) && name.equals("place")) {
                	
                    int size = parser.getAttributeCount();
                    
                    for(int i = 0; i < size; i++) {
                    	
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        
                        if((attrName != null) && attrName.equals("id")) {      	
                            id = new Integer(attrValue);       
                        } else if ((attrName != null) && attrName.equals("name")) {
                            placeName = attrValue;
                        } else if ((attrName != null) && attrName.equals("lat")) {	
                            lat = new Double(attrValue);    
                        } else if ((attrName != null) && attrName.equals("lon")) { 	
                            lon = new Double(attrValue); 
                        } else if ((attrName != null) && attrName.equals("action")) { 	
                            action = attrValue; 
                        }
                        else if ((attrName != null) && attrName.equals("desc")) { 	
                            desc = attrValue; 
                        }
                    }
                    
                    if((id != -1) && (placeName != null)) {
                        
                    	
                    	auxPlace = new Place(id, placeName, lat, lon, false, action, desc);
                    	
                    	if(auxPlace.action.contains("food")){
                    		Utils.eatingPlaces[eatingPlacesCount] = auxPlace;
                    		eatingPlacesCount++;
                    	}
                    	else if(auxPlace.action.equalsIgnoreCase("beg")){
                    		Utils.beggingPlaces[beggingPlacesCount] = auxPlace;
                    		beggingPlacesCount++;
                    	}
                    	else if(auxPlace.action.equalsIgnoreCase("sell hus forbi")){
                    		Utils.husForbiPlaces[husForbiPlacesCount] = auxPlace;
                    		husForbiPlacesCount++;
                    	}
                    	else if(auxPlace.action.equalsIgnoreCase("sleep in the street")){
                    		Utils.sleepingPlaces[sleepingPlacesCount] = auxPlace;
                    		sleepingPlacesCount++;
                    	}
                    	else if(auxPlace.action.contains("trash")){
                    		Utils.trashCansPlaces[trashCansPlacesCount] = auxPlace;
                    		trashCansPlacesCount++;
                    	}
                        
                        //Log.d("L.PLACES XML", placeName + ", "+ action + ", " + lat + ", " + lon);
                        
                        counter++;
                    }
                    
                   
                }
                
            }
            
            

            
        }catch(Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        
        Log.d("eatingPlacesCount: ", eatingPlacesCount + "");
        Log.d("beggingPlacesCount: ", beggingPlacesCount + "");
        Log.d("husForbiPlacesCount: ", husForbiPlacesCount + "");
        Log.d("sleepingPlacesCount: ", sleepingPlacesCount + "");
        Log.d("trashCansPlacesCount: ", trashCansPlacesCount + "");
        
    }
	
	public static void loadEventsFromXML(Context pContext){
    	
		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
		
		Event auxEvent = new Event();
	    XmlPullParser parser = pContext.getResources().getXml(R.xml.eventsandstories);
	    int counter = 0;
	    
        try {
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
            	
                String name = parser.getName();
                int id = -1;
                String eventName = null;
                float freq = -1;
                Boolean achieved = false;
                String description = "";
                String type = "";
                
                if((name != null) && name.equals("event")) {
                	
                    int size = parser.getAttributeCount();
                    
                    for(int i = 0; i < size; i++) {
                    	
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        
                        if((attrName != null) && attrName.equals("id")) {      	
                            id = new Integer(attrValue);       
                        } else if ((attrName != null) && attrName.equals("name")) {
                            eventName = attrValue;
                        } else if ((attrName != null) && attrName.equals("freq")) {	
                            freq = Float.valueOf(attrValue);   
                        } else if ((attrName != null) && attrName.equals("type")) {	
                            type = attrValue; 
                        } else if ((attrName != null) && attrName.equals("achieved")) { 	
                            achieved = new Boolean(attrValue); 
                        } else if ((attrName != null) && attrName.equals("description")) { 	
                            description = attrValue; 
                        }
                    }
                    
                    if((id != -1) && (eventName != null)) {
                    	
                    	Utils.currentDay = preferences.getInt("totalDays", 0);
                    	Log.d("loaded total days: ", Utils.currentDay+"");
                        
                    	Boolean aux = preferences.getBoolean("e" + id + "Achieved", false);
                    	
                    	auxEvent = new Event(id, eventName, type, freq, aux, description);
                        
                    	if(eventName != null) {    
                            Utils.globalEvents[counter] = auxEvent;
                            counter++;
                            Utils.totalEvents = counter;
                        }   
                        //Log.d("L.PLACES XML", placeName + ", "+ action + ", " + lat + ", " + lon);
                        
                    }
                    
                   
                }
                
            }
            
            Log.d("Total events", Utils.totalEvents + "");
            
        }catch(Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        
        Log.d("eatingPlacesCount: ", eatingPlacesCount + "");
        Log.d("beggingPlacesCount: ", beggingPlacesCount + "");
        Log.d("husForbiPlacesCount: ", husForbiPlacesCount + "");
        Log.d("sleepingPlacesCount: ", sleepingPlacesCount + "");
        Log.d("trashCansPlacesCount: ", trashCansPlacesCount + "");
        
    }
	
	public static String[] loadedStories(Context pContext){
		
		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
		
		String[] Stories = new String[30];
		
		for(int i = 0; i < Utils.currentDay; i++){
			Stories[i] = preferences.getString("d" + i + "Story", "");
		}
		
		return Stories;
		
		
	}
	
	public static void loadMemoriesFromXML(Context pContext){
    	
	    XmlPullParser parser = pContext.getResources().getXml(R.xml.memories);
	    int counter = 0;
	    
        try {
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
            	
                String tagName = parser.getName();
                
                String memoryName = null;
                Boolean achieved = false;
                String description = "";
                
                
                if((tagName != null) && tagName.equals("memory")) {
                	
                    int size = parser.getAttributeCount();
                    
                    for(int i = 0; i < size; i++) {
                    	
                        String attrName = parser.getAttributeName(i);
                        String attrValue = parser.getAttributeValue(i);
                        
                        if ((attrName != null) && attrName.equals("name")) {
                            memoryName = attrValue;
                        } else if ((attrName != null) && attrName.equals("achieved")) {	
                            achieved = new Boolean(attrValue);    
                        } else if ((attrName != null) && attrName.equals("description")) { 	
                            description = new String(attrValue); 
                        }
                    }
                    
                    if(memoryName != null) {    
                        Utils.globalMemories[counter] = new Memory(memoryName, achieved, description);
                        counter++;
                        Utils.totalMemories = counter;
                    }                  
                }            
            }
            
            Log.d("Total memories", Utils.totalMemories + "");
            
        }catch(Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        
        
    }
	
//	public static Action getActionByName(String seekName) {
//		
//		
//		Log.d("ACT.BY NAME","seeking: " + seekName + " in " + totalActions + " last: " + globalActions[totalActions-1].name);
//		
//		for(int i=0; i < totalActions; i++){
//			
//			Log.d("ACT.BY NAME","checking act: " + globalActions[i].name);
//			
//			if(seekName.equalsIgnoreCase(globalActions[i].name)){
//				Log.d("ACT.BY NAME","FOUND!");
//				return globalActions[i];
//			}
//			
//		}
////		Log.d("ACT.BY NAME","act. not found");
//		return new Action();
//	}
//	
//	public static void computeActionChanges(Action performedAction){
//
//		player.actionsPerformed[player.countActionsDone] = performedAction;
//		player.countActionsDone++;
//		
////		Log.d("TAKE ACT","done: " + player.countActionsDone);
//		
//		// ACTION MODIFIERS
//		player.mentalHealth += performedAction.mMH;
//		player.physicalHealth += performedAction.mPH;
//		player.mood += performedAction.mMO;
//		player.money += performedAction.mCA;
//		
//		Log.d("MOOD", player.mood +"");
//		
//		switch(currentTimeOfDay){
//			
//		case MORNING:
//			
//			player.dayStory = player.dayStory + " In the morning, " + performedAction.storyBit.toLowerCase() 
//				+ storyConnector(performedAction);
//			currentTimeOfDay = AFTERNOON;	
////			Log.d("TAKE ACT","morning: " + player.dayStory);
//			Log.d("STORY",player.dayStory);
//			break;
//			
//		case AFTERNOON:
//			player.dayStory = player.dayStory + " In the afternoon, " + performedAction.storyBit.toLowerCase() 
//				+ storyConnector(performedAction);
//			currentTimeOfDay = EVENING;	
//			Log.d("STORY",player.dayStory);
//			break;
//			
//		case EVENING:
//			player.dayStory = player.dayStory + " In the evening, " + performedAction.storyBit.toLowerCase() 
//				+ storyConnector(performedAction);
//			currentTimeOfDay = NIGHT;
//			Log.d("STORY",player.dayStory);
//			break;
//			
//		case NIGHT:
//			player.dayStory = player.dayStory + " In the night, " + performedAction.storyBit.toLowerCase() 
//				+ storyConnector(performedAction);
//			Log.d("STORY",player.dayStory);
//			
//			player.overallStory = player.overallStory + " Day " + currentDay + ":" + player.dayStory;
//			
//			Log.d("O. STORY",player.overallStory);
//			
//			gamePhase = END_OF_DAY;
//		
//			break;
//		}
//			
//	}
//	
//	public static void endDayDegradation(){
//		
//		player.physicalHealth -= 15;
//		player.mentalHealth -= 10;
//		player.mood -= 15;
//		
//	}
//	
//	public static String storyConnector(Action last){
//		
//		int randomIndex = (int)(Math.random()*9);
//		String connector = new String();
//		
//		Log.d("RANDOM",randomIndex+"");
//		
//		// PLAYER IS HAPPY
//		if(player.mood >= 70){
//			
//			if(last.type.equals("work")){
//				connector = workHappyConnectors[randomIndex];
//			}
//			else if(last.type.equals("fun")){
//				connector = funHappyConnectors[randomIndex];
//			}
//			else if(last.type.equals("sleep")){
//				connector = sleepHappyConnectors[randomIndex];
//			}
//			else if(last.type.equals("eat")){
//				connector = eatHappyConnectors[randomIndex];
//			}
//			
//		}
//		// PLAYER IS NORMAL
//		else if(player.mood >= 40){
//			
//			if(last.type.equals("work")){
//				connector = workNormalConnectors[randomIndex];
//			}
//			else if(last.type.equals("fun")){
//				connector = funNormalConnectors[randomIndex];
//			}
//			else if(last.type.equals("sleep")){
//				connector = sleepNormalConnectors[randomIndex];
//			}
//			else if(last.type.equals("eat")){
//				connector = eatNormalConnectors[randomIndex];
//			}
//			
//		}
//		// PLAYER IS SAD
//		else {
//			
//		}
//		
//		
//		
//		
//		return connector;
//		
//	}
//	
	public static void saveEventsPrefs(Context pContext){
		
		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
        
		for(int i = 0; i < totalEvents; i++){
			
			preferences.edit().putBoolean("e" + globalEvents[i].id + "Achieved", globalEvents[i].achieved).commit();

		}
		
		int aux = Utils.currentDay + 1;
		
		preferences.edit().putString("d" + Utils.currentDay + "Story", Utils.player.dayStory).commit();
		
		preferences.edit().putInt("totalDays", Utils.currentDay).commit();
		Log.d("S.EVENTS PREFS", "SAVED");
	}
//	
//	public static void loadPlacesFromPrefs(Context pContext) {
//		
//		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
//        
//		Utils.globalPlaces = new Place[100];
//		Utils.countPlaces = 0;
//		
//		//Log.d("L.PLACES PREFS", "ENTERED function: " + preferences.getString("p" + i + "Name",""));
//		
//		XmlPullParser parser = pContext.getResources().getXml(R.xml.places);
//	    int counter = 0;
//	    
//        try {
//            while (parser.next() != XmlPullParser.END_DOCUMENT) {
//            	
//                String name = parser.getName();
//                int id = -1;
//                String placeName = null;
//                Double lat = -1.0;
//                Double lon = -1.0;
//                String action = "", desc = "";
//                
//                if((name != null) && name.equals("place")) {
//                	
//                    int size = parser.getAttributeCount();
//                    
//                    for(int i = 0; i < size; i++) {
//                    	
//                        String attrName = parser.getAttributeName(i);
//                        String attrValue = parser.getAttributeValue(i);
//                        
//                        if((attrName != null) && attrName.equals("id")) {      	
//                            id = new Integer(attrValue);       
//                        } else if ((attrName != null) && attrName.equals("name")) {
//                            placeName = attrValue;
//                        } else if ((attrName != null) && attrName.equals("lat")) {	
//                            lat = new Double(attrValue);    
//                        } else if ((attrName != null) && attrName.equals("lon")) { 	
//                            lon = new Double(attrValue); 
//                        } else if ((attrName != null) && attrName.equals("action")) { 	
//                            action = attrValue; 
//                        }
//                        else if ((attrName != null) && attrName.equals("desc")) { 	
//                            desc = attrValue; 
//                        }
//                    }
//                    
//                    if((id != -1) && (placeName != null)) {
//                        Log.d("L.PLACES XML", id + ", " + placeName + ", " + lat + ", " + lon + "\n");
//                        Utils.globalPlaces[counter] = new Place(id, placeName, lat, lon, false, action, desc);
//                        Utils.globalPlaces[counter].clickable = preferences.getBoolean("p" + counter + "Clickable", false);
//                        counter++;
//                        Utils.countPlaces = counter;
//                    }
//                    
//                   
//                }
//                
////                textView.setText(stringBuilder.toString());
//            }
//            
//        }catch(Exception e) {
//            Log.e("ERROR", e.getMessage());
//        }
//
//	}
//	
//	public static void savePlayerPrefs(Context pContext){
//		
//		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
//	
//		preferences.edit().putFloat("playerPH", player.physicalHealth).commit();
//		preferences.edit().putFloat("playerMH", player.mentalHealth).commit();
//		preferences.edit().putFloat("playerMO", player.mood).commit();
//		preferences.edit().putFloat("playerCA", player.money).commit();
//		
//		preferences.edit().putInt("playerPosLat", player.pos.getLatitudeE6()).commit();
//		preferences.edit().putInt("playerPosLon", player.pos.getLongitudeE6()).commit();
//		
//		preferences.edit().putString("playerDayStory", player.dayStory).commit();
//		preferences.edit().putString("playerOverallStory", player.overallStory).commit();
//		
//		Log.d("S.PLAYER PREFS", "SAVED");
//
//	}
//	
//	public static void loadPlayerPrefs(Context pContext){
//		
//		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
//		
//		player.physicalHealth   = preferences.getFloat("playerPH", 50);
//		player.mentalHealth  	= preferences.getFloat("playerMH", 50);
//		player.mood  			= preferences.getFloat("playerMO", 50);
//		player.money  			= preferences.getFloat("playerCA", 50);
//		
//		player.dayStory			= preferences.getString("playerDayStory", "");
//		player.overallStory		= preferences.getString("playerOverallStory", "");
//		
//		player.pos = new GeoPoint(preferences.getInt("playerPosLat", 0), preferences.getInt("playerPosLon", 0));
//		
//		Log.d("L.PLAYER PREFS", "LOADED");
//		
//	}
//
//	public static void savePlaystatePrefs(Context pContext){
//		
//		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
//		
//		preferences.edit().putInt("timeOfDay", currentTimeOfDay).commit();
//		preferences.edit().putInt("day", currentDay).commit();
//		
//		Log.d("S.PLAYST. PREFS", "SAVED");
//		
//	}
//	
//	public static void loadPlaystatePrefs(Context pContext){
//		
//		preferences  = pContext.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
//		
//		currentDay 			= preferences.getInt("day", 1);
//		currentTimeOfDay	= preferences.getInt("timeOfDay", MORNING);
//
//		Log.d("L.PLAYST. PREFS", "LOADED");
//	}

	
//    
//    public static void loadActionsFromXML(Context pContext){
//    	
//	    XmlPullParser parser = pContext.getResources().getXml(R.xml.gamelogicparams);
//	    
//	    int counter = 0;
//	    boolean done = false;
//	    
//	    boolean isActions = false;
//	    
//	    int eventType; 
//	    
//	    
//        try {
//        	
//        	eventType = parser.getEventType();
//        	
//            while (eventType != XmlPullParser.END_DOCUMENT && !done){
//                
//            	String name = null;
//                
//                switch (eventType){
//                    
//                	case XmlPullParser.START_DOCUMENT:
//                        break;
//                    
//                    case XmlPullParser.START_TAG:
//                        
//                    	name = parser.getName();
//                    	
//                    	if (name.equalsIgnoreCase("action")){
//                            Utils.globalActions[counter] = new Action();
//                            Log.d("L.ACTIONS XML", "NEW ACTION " + counter);
//                            isActions = true;
//                            
//                            // Loads attributes
//                            int size = parser.getAttributeCount();
//                            for(int i = 0; i < size; i++) {
//                            	
//                                String attrName = parser.getAttributeName(i);
//                                String attrValue = parser.getAttributeValue(i);
//                                
//                                if((attrName != null) && attrName.equals("id")) {      	
//                                	Utils.globalActions[counter].id = new Integer(attrValue);       
//                                } else if ((attrName != null) && attrName.equals("name")) {
//                                    Utils.globalActions[counter].name = attrValue;
//                                }
//                            }
//                        } 
//                    	else if (Utils.globalActions[counter] != null && isActions){
//                            
//                        	if (name.equalsIgnoreCase("desc")){
//                            	Utils.globalActions[counter].desc = (parser.nextText());
//                            } else if (name.equalsIgnoreCase("is_place")){
//                            	Utils.globalActions[counter].isPlace = (parser.nextText());
//                            } else if (name.equalsIgnoreCase("is_event")){
//                            	Utils.globalActions[counter].isEvent = (parser.nextText());
//                            } else if (name.equalsIgnoreCase("type")){
//                            	Utils.globalActions[counter].type = (parser.nextText());
//	                        } else if (name.equalsIgnoreCase("m_ph")){
//	                        	Utils.globalActions[counter].mPH = Float.valueOf(parser.nextText());
//			                } else if (name.equalsIgnoreCase("m_mh")){
//			                	Utils.globalActions[counter].mMH = Float.valueOf(parser.nextText());
//			                } else if (name.equalsIgnoreCase("m_mo")){
//			                	Utils.globalActions[counter].mMO = Float.valueOf(parser.nextText());
//			                } else if (name.equalsIgnoreCase("m_ca")){
//			                	Utils.globalActions[counter].mCA = Float.valueOf(parser.nextText());
//			                } else if (name.equalsIgnoreCase("story_bit")){
//			                	Utils.globalActions[counter].storyBit = (parser.nextText());
//			                }
//                        }
//
//                        break;
//                    
//                    case XmlPullParser.END_TAG:
//                        
//                    	name = parser.getName();
//                    	
//                    	if (name.equalsIgnoreCase("action") && Utils.globalActions[counter] != null){
////                    		Log.d("GL-XML","AC"+ Utils.globalActions[counter].id + " " + Utils.globalActions[counter].name + ": " + Utils.globalActions[counter].storyBit);
//                    		counter++;
//                            isActions = false;
//                            
//                        } 
//
//                        break;
//                }
//                eventType = parser.next();
//            }
//            
//            Utils.totalActions = counter;
//            Log.d("L.ACTIONS XML","TOTAL: " + Utils.totalActions);
//            
//        }catch(Exception e) {
//            Log.e("ERROR", e.getMessage());
//        }
//        
//        
//    }
//    
//    public static void loadPlayerFromXML(Context pContext){
//    	
//    	XmlPullParser parser = pContext.getResources().getXml(R.xml.gamelogicparams);
//
//	    boolean done = false;
//	    
//	    boolean isPlayer = false;
//	    
//	    int eventType; 
//	    
//	    
//        try {
//        	
//        	eventType = parser.getEventType();
//        	
//            while (eventType != XmlPullParser.END_DOCUMENT && !done){
//                
//            	String name = null;
//                
//                switch (eventType){
//                    
//                	case XmlPullParser.START_DOCUMENT:
//                        break;
//                    
//                    case XmlPullParser.START_TAG:
//                        
//                    	name = parser.getName();
//                    	
//                    	if (name.equalsIgnoreCase("player")){
//                            Log.d("L.PLAYER XML", "LOADED");
//                            isPlayer = true;
//                        } 
//                    	else if (isPlayer){
//                            
//                        	if (name.equalsIgnoreCase("physical_health")){
//                            	Utils.player.physicalHealth = Float.valueOf(parser.nextText());
//                            } else if (name.equalsIgnoreCase("mental_health")){
//                            	Utils.player.mentalHealth = Float.valueOf(parser.nextText());
//                            } else if (name.equalsIgnoreCase("money")){
//                            	Utils.player.money = Float.valueOf(parser.nextText());
//                            } else if (name.equalsIgnoreCase("mood")){
//                            	Utils.player.mood = Float.valueOf(parser.nextText());
//	                        }
//                        }
//
//                        break;
//                    
//                    case XmlPullParser.END_TAG:
//                        
//                    	name = parser.getName();
//                    	
//                    	if (name.equalsIgnoreCase("player")){
////                            Log.d("GL-XML","PL: " + Utils.player.mentalHealth);
//                    		isPlayer = false;
//                        } 
//       
//                        break;
//                }
//                eventType = parser.next();
//            }
//            
////            Log.d("GL-XML","LOADED NEW PLAYER: " + Utils.player.physicalHealth);
//            
//        }catch(Exception e) {
//            Log.e("ERROR", e.getMessage());
//        }
//    }
//
//    public static void loadPlaystateFromXML (Context pContext){
//    	
//    	XmlPullParser parser = pContext.getResources().getXml(R.xml.gamelogicparams);
//	    boolean done = false;
//	    
//	    boolean isPlaystate = false;
//	    
//	    int eventType; 
//	    
//	    
//        try {
//        	
//        	eventType = parser.getEventType();
//        	
//            while (eventType != XmlPullParser.END_DOCUMENT && !done){
//                
//            	String name = null;
//                
//                switch (eventType){
//                    
//                	case XmlPullParser.START_DOCUMENT:
//                        break;
//                    
//                    case XmlPullParser.START_TAG:
//                        
//                    	name = parser.getName();
//                        
//                    	if (name.equalsIgnoreCase("playstate")){
//                            Log.d("L.PLAYST. XML", "LOADED");
//                            isPlaystate = true;
// 
//                        } 
//                    	else if (isPlaystate){
//                            
//                        	if (name.equalsIgnoreCase("food_degrade")){
//                            	Utils.foodDegradation = Float.valueOf(parser.nextText());
//                            } else if (name.equalsIgnoreCase("sleep_degrade")){
//                            	Utils.sleepDegradation = Float.valueOf(parser.nextText());
//                            } else if (name.equalsIgnoreCase("min_ph")){
//                            	Utils.minPH = Float.valueOf(parser.nextText());
//                            } else if (name.equalsIgnoreCase("min_mh")){
//                            	Utils.minMH = Float.valueOf(parser.nextText());
//	                        } else if (name.equalsIgnoreCase("min_mo")){
//			                	Utils.minMO = Float.valueOf(parser.nextText());
//			                }
//                        }
//
//                        break;
//                    
//                    case XmlPullParser.END_TAG:
//                        
//                    	name = parser.getName();
//                    	
//                    	if (name.equalsIgnoreCase("playstate")){
////                            Log.d("GL-XML","PS: " + Utils.foodDegradation + " " + Utils.sleepDegradation);
//                    		isPlaystate = false;
//                        } 
//
//                        break;
//                }
//                eventType = parser.next();
//            }
//            
////            Log.d("GL-XML","LOADED ACTIONS TOTAL: " + Utils.totalActions);
//            
//        }catch(Exception e) {
//            Log.e("ERROR", e.getMessage());
//        }
//    }
}
